//
//  WmcDevNetworkApiDetailVM.swift
//  YPWatermarkCamera
//
//  Created by 周诗松 on 2022/6/27.
//

import UIKit

class WmcDevNetworkApiDetailVM: WmcBaseVM {
    // Internal

    class WmcParamsModel {
        // Lifecycle

        convenience init(key: String, value: String, isRequired: Bool, desc: String) {
            self.init()
            self.key = key
            self.value = value
            self.isRequired = isRequired
            self.desc = desc
        }

        convenience init(key: String, isRequired: Bool, desc: String) {
            self.init()
            self.key = key
            self.isRequired = isRequired
            self.desc = desc
        }

        // Internal

        var key: String = ""
        var value: String = ""
        var isRequired: Bool = false
        var desc: String = ""
    }

    var model: WmcDevNetworkApiListVM.WmcApiModel = .init()

    private(set) lazy var dataSource: [WmcParamsModel] = {
        var array: [WmcParamsModel] = []
        if model.path == "/media/v1/resource/applyUpload" {
            array = api_1
        } else if model.path == "/media/v1/resource/applyBatchUpload" {
            array = api_2
        } else if model.path == "/media/v1/resource/applyCopy" {
            array = api_3
        } else if model.path == "/media/v1/resource/access" {
            array = api_4
        } else if model.path == "/media/v1/app/getConfigs" {
            array = api_5
        } else if model.path == "/media/v1/watermark/listWatermarks" {
            array = api_6
        } else if model.path == "/media/v1/brand/search" {
            array = api_7
        } else if model.path == "/media/v1/watermark/share" {
            array = api_8
        } else if model.path == "/media/v1/watermark/getWmInfo" {
            array = api_9
        } else if model.path == "/media/v1/album/applyUpload" {
            array = api_10
        } else if model.path == "/media/v1/album/list" {
            array = api_11
        } else if model.path == "/media/v1/album/delete" {
            array = api_12
        } else if model.path == "/media/v1/album/getTakeDate" {
            array = api_13
        } else if model.path == "/media/v1/album/classifyList" {
            array = api_14
        } else if model.path == "/media/v1/album/watermarkList" {
            array = api_15
        } else if model.path == "/media/v1/album/shareAlbum" {
            array = api_16
        } else if model.path == "/media/v1/album/getPreDate" {
            array = api_17
        }
        return array
    }()

    func onRequest() {
        if !checkoutRequired() {
            WmcHudManager.toast("请检查必选参数")
            return
        }

        if model.path == "/media/v1/resource/applyUpload" {
            fetchApplyUpload()
        } else if model.path == "/media/v1/resource/applyBatchUpload" {
            fetchBatchApplyUpload()
        } else if model.path == "/media/v1/resource/applyCopy" {
            fetchApplyCopy()
        } else if model.path == "/media/v1/resource/access" {
            fetchResourceAccess()
        } else if model.path == "/media/v1/app/getConfigs" {
            fetchConfigs()
        } else if model.path == "/media/v1/watermark/listWatermarks" {
            fetchWatermarkList()
        } else if model.path == "/media/v1/brand/search" {
            fetchBranchSearch()
        } else if model.path == "/media/v1/watermark/share" {
            fetchWmShare()
        } else if model.path == "/media/v1/watermark/getWmInfo" {
            fetchWmTemple()
        } else if model.path == "/media/v1/album/applyUpload" {
            fetchImageUploadApply()
        } else if model.path == "/media/v1/album/list" {
            fetchAlbumList()
        } else if model.path == "/media/v1/album/delete" {
            deleteImage()
        } else if model.path == "/media/v1/album/getTakeDate" {
            fetchTakeDate()
        } else if model.path == "/media/v1/album/classifyList" {
            fetchClassifyAlbum()
        } else if model.path == "/media/v1/album/watermarkList" {
            fetchAlbumWatermarkList()
        } else if model.path == "/media/v1/album/shareAlbum" {
            saveShareInfo()
        } else if model.path == "/media/v1/album/getPreDate" {
            fetchLastestTakeDate()
        }
    }

    func dictConvertToString(dict: [String: Any]) -> String {
        var result: String = ""
        do {
            let jsonData = try JSONSerialization.data(withJSONObject: dict, options: JSONSerialization.WritingOptions(rawValue: 0))

            if let JSONString = String(data: jsonData, encoding: String.Encoding.utf8) {
                result = JSONString
            }

        } catch {
            result = ""
        }
        return result
    }

    // Private

    private lazy var api_1: [WmcParamsModel] = {
        let uid = WmcLoginManager.shared.yupaoAccountModel?.userId ?? ""
        let params_1: WmcParamsModel = .init(key: "entryId", value: "100002", isRequired: true, desc: "入口场景id")
        let params_2: WmcParamsModel = .init(key: "type", value: "0", isRequired: true, desc: "资源类型，0:图片，1视频，2音频")
        let params_3: WmcParamsModel = .init(key: "format", value: "png", isRequired: true, desc: "文件格式名")
        let params_4: WmcParamsModel = .init(key: "takeTime", isRequired: false, desc: "拍摄时间")
        let params_5: WmcParamsModel = .init(key: "city", isRequired: false, desc: "城市")
        let params_6: WmcParamsModel = .init(key: "address", isRequired: false, desc: "地址详情")
        let params_7: WmcParamsModel = .init(key: "wmId", isRequired: false, desc: "水印类型，2音频可以为空")
        let params_8: WmcParamsModel = .init(key: "wmcInfo", isRequired: false, desc: "水印相关扩展信息，2音频可以为空")
        let params_9: WmcParamsModel = .init(key: "uid", value: uid, isRequired: false, desc: "鱼泡uid")
        let params_10: WmcParamsModel = .init(key: "httpMethod", isRequired: false, desc: "http上传方式，0: put，1: post，默认是0")

        let result = [params_1, params_2, params_3, params_4, params_5, params_6, params_7, params_8, params_9, params_10]
        return result
    }()

    private lazy var api_2: [WmcParamsModel] = {
        let uid = WmcLoginManager.shared.yupaoAccountModel?.userId ?? ""
        let params_1: WmcParamsModel = .init(key: "entryId", value: "100002", isRequired: true, desc: "入口场景id")
        let params_2: WmcParamsModel = .init(key: "type", value: "0", isRequired: true, desc: "资源类型，0:图片，1视频，2音频")
        let params_3: WmcParamsModel = .init(key: "format", value: "png", isRequired: true, desc: "文件格式名")
        let params_4: WmcParamsModel = .init(key: "takeTime", isRequired: false, desc: "拍摄时间")
        let params_5: WmcParamsModel = .init(key: "city", isRequired: false, desc: "城市")
        let params_6: WmcParamsModel = .init(key: "address", isRequired: false, desc: "地址详情")
        let params_7: WmcParamsModel = .init(key: "wmId", isRequired: false, desc: "水印类型，2音频可以为空")
        let params_8: WmcParamsModel = .init(key: "wmcInfo", isRequired: false, desc: "水印相关扩展信息，2音频可以为空")
        let params_9: WmcParamsModel = .init(key: "uid", value: uid, isRequired: false, desc: "鱼泡uid")
        let params_10: WmcParamsModel = .init(key: "httpMethod", isRequired: false, desc: "http上传方式，0: put，1: post，默认是0")

        let result = [params_1, params_2, params_3, params_4, params_5, params_6, params_7, params_8, params_9, params_10]
        return result
    }()

    private lazy var api_3: [WmcParamsModel] = {
        let uid = WmcLoginManager.shared.yupaoAccountModel?.userId ?? ""
        let params_1: WmcParamsModel = .init(key: "entryId", value: "100002", isRequired: true, desc: "入口场景id")
        let params_2: WmcParamsModel = .init(key: "type", value: "0", isRequired: true, desc: "资源类型，0:图片，1视频，2音频")
        let params_3: WmcParamsModel = .init(key: "format", value: "png", isRequired: true, desc: "文件格式名")
        let params_4: WmcParamsModel = .init(key: "takeTime", value: "2022-06-01", isRequired: true, desc: "拍摄时间")
        let params_5: WmcParamsModel = .init(key: "accessUrl", value: "", isRequired: true, desc: "资源唯一id")
        let params_6: WmcParamsModel = .init(key: "copSign", value: "", isRequired: true, desc: "授权复制的鉴权，生成规则详见：https://w3nu1yaadv.feishu.cn/wiki/wikcnAB4sEYQbUwL8QbXCbp5THf#")
        let params_7: WmcParamsModel = .init(key: "city", isRequired: false, desc: "城市")
        let params_8: WmcParamsModel = .init(key: "address", isRequired: false, desc: "地址详情")
        let params_9: WmcParamsModel = .init(key: "wmId", value: "", isRequired: false, desc: "水印类型，2音频可以为空")
        let params_10: WmcParamsModel = .init(key: "wmcInfo", isRequired: false, desc: "水印相关扩展信息，2音频可以为空")

        let result = [params_1, params_2, params_3, params_4, params_5, params_6, params_7, params_8, params_9, params_10]
        return result
    }()

    private lazy var api_4: [WmcParamsModel] = {
        let params_1: WmcParamsModel = .init(key: "url_1", value: "", isRequired: false, desc: "需要访问的资源地址")
        let params_2: WmcParamsModel = .init(key: "url_2", value: "", isRequired: false, desc: "需要访问的资源地址")
        let params_3: WmcParamsModel = .init(key: "url_3", value: "", isRequired: false, desc: "需要访问的资源地址")
        let params_4: WmcParamsModel = .init(key: "url_4", value: "", isRequired: false, desc: "需要访问的资源地址")
        let params_5: WmcParamsModel = .init(key: "url_5", value: "", isRequired: false, desc: "需要访问的资源地址")

        let result = [params_1, params_2, params_3, params_4, params_5]
        return result
    }()

    private lazy var api_5: [WmcParamsModel] = {
        return []
    }()

    private lazy var api_6: [WmcParamsModel] = {
        return []
    }()

    private lazy var api_7: [WmcParamsModel] = {
        let params_1: WmcParamsModel = .init(key: "currentPage", value: "1", isRequired: true, desc: "当前页，从1开始")
        let params_2: WmcParamsModel = .init(key: "pageSize", value: "10", isRequired: true, desc: "页大小，最小10，最大50")
        let params_3: WmcParamsModel = .init(key: "name", value: "", isRequired: false, desc: "搜索关键字")

        let result = [params_1, params_2, params_3]
        return result
    }()

    private lazy var api_8: [WmcParamsModel] = {
        let dict: [String: Any] = [
            "wmId": 49,
            "classifyId": 17,
            "name": "内务检查",
            "opacity": 0,
            "size": 50,
            "img": "http://cdn.cdypkj.cn/wmc/template/35021648720190.png",
            "icon": "http://cdn.cdypkj.cn/wmc/icon/62261648720827.png",
            "supportAlbum": false,
            "styleCode": 10,
            "sort": 5,
            "fields": [[
                "fieldId": 458,
                "title": "标题",
                "content": "内务检查",
                "editTitle": false,
                "editContent": true,
                "type": "title",
                "isOpen": true,
                "isAction": false,
                "textColor": "",
                "bgColor": "",
                "sort": 1,
                "brandAttribute": "null",
            ]],
        ]
        
        let json = dictConvertToString(dict: dict)

        let params_1: WmcParamsModel = .init(key: "wmcInfo", value: json, isRequired: false, desc: "水印模板信息json")
        let params_2: WmcParamsModel = .init(key: "wmUrl", value: "http://cdn.cdypkj.cn/wmc/template/93211648460369.png", isRequired: false, desc: "水印模板图片url")

        let result = [params_1, params_2]
        return result
    }()

    private lazy var api_9: [WmcParamsModel] = {
        let params_1: WmcParamsModel = .init(key: "code", value: "brand_share_code_13db3eb7-9938-48d1-ab4e-745369ec3dd7", isRequired: false, desc: "code")

        let result = [params_1]
        return result
    }()

    private lazy var api_10: [WmcParamsModel] = {
        let uid = WmcLoginManager.shared.yupaoAccountModel?.userId ?? ""

        let params_1: WmcParamsModel = .init(key: "albumType", value: "0", isRequired: true, desc: "相册类型，0：个人")
        let params_2: WmcParamsModel = .init(key: "busId", value: WmcLoginManager.shared.yupaoAccountModel?.userId ?? "", isRequired: true, desc: "用户id，albumType为0时是鱼泡用户id")
        let params_3: WmcParamsModel = .init(key: "entryId", value: "100002", isRequired: true, desc: "入口场景id")
        let params_4: WmcParamsModel = .init(key: "wmId", value: "0", isRequired: true, desc: "水印类型，2音频可以为空")
        let params_5: WmcParamsModel = .init(key: "city", isRequired: false, desc: "城市")
        let params_6: WmcParamsModel = .init(key: "address", isRequired: false, desc: "地址详情")
        let params_7: WmcParamsModel = .init(key: "takeTime", isRequired: false, desc: "拍摄时间")
        let params_8: WmcParamsModel = .init(key: "wmcInfo", isRequired: false, desc: "水印相关扩展信息，2音频可以为空")
        let params_9: WmcParamsModel = .init(key: "ip", value: "", isRequired: false, desc: "ip")
        let params_10: WmcParamsModel = .init(key: "type", value: "0", isRequired: true, desc: "资源类型，0:图片，1视频，2音频")
        let params_11: WmcParamsModel = .init(key: "format", value: "png", isRequired: true, desc: "文件格式名")

        let result = [params_1, params_2, params_3, params_4, params_5, params_6, params_7, params_8, params_9, params_10, params_11]
        return result
    }()

    private lazy var api_11: [WmcParamsModel] = {
        let params_1: WmcParamsModel = .init(key: "albumType", value: "0", isRequired: true, desc: "相册类型，0：个人")
        let params_2: WmcParamsModel = .init(key: "busId", value: WmcLoginManager.shared.yupaoAccountModel?.userId ?? "", isRequired: true, desc: "用户id，albumType为0时是鱼泡用户id")
        let params_3: WmcParamsModel = .init(key: "wmIds", value: "", isRequired: false, desc: "水印类型id")
        let params_4: WmcParamsModel = .init(key: "date", value: "", isRequired: false, desc: "yyyy-MM-dd格式")
        let params_5: WmcParamsModel = .init(key: "currentPage", value: "1", isRequired: true, desc: "当前页号")
        let params_6: WmcParamsModel = .init(key: "pageSize", value: "10", isRequired: true, desc: "分页大小")
        let params_7: WmcParamsModel = .init(key: "resourceType", value: "", isRequired: false, desc: "资源类型 0：图片  1：视频   不传：所有")

        let result = [params_1, params_2, params_3, params_4, params_5, params_6, params_7]
        return result
    }()

    private lazy var api_12: [WmcParamsModel] = {
        let params_1: WmcParamsModel = .init(key: "albumType", value: "0", isRequired: true, desc: "相册类型，0：个人")
        let params_2: WmcParamsModel = .init(key: "busId", value: WmcLoginManager.shared.yupaoAccountModel?.userId ?? "", isRequired: true, desc: "用户id，albumType为0时是鱼泡用户id")
        let params_3: WmcParamsModel = .init(key: "id", value: "", isRequired: true, desc: "相册资源id")

        let result = [params_1, params_2, params_3]
        return result
    }()

    private lazy var api_13: [WmcParamsModel] = {
        let params_1: WmcParamsModel = .init(key: "albumType", value: "0", isRequired: true, desc: "相册类型，0：个人")
        let params_2: WmcParamsModel = .init(key: "busId", value: WmcLoginManager.shared.yupaoAccountModel?.userId ?? "", isRequired: true, desc: "用户id，albumType为0时是鱼泡用户id")
        let params_3: WmcParamsModel = .init(key: "startDate", value: "2022-01-01", isRequired: true, desc: "开始时间 yyyy-MM-dd格式")
        let params_4: WmcParamsModel = .init(key: "endDate", value: "2022-06-01", isRequired: true, desc: "结束时间 yyyy-MM-dd格式")
        let params_5: WmcParamsModel = .init(key: "resourceType", value: "", isRequired: false, desc: "资源类型 0：图片  1：视频   不传：所有")

        let result = [params_1, params_2, params_3, params_4, params_5]
        return result
    }()

    private lazy var api_14: [WmcParamsModel] = {
        let params_1: WmcParamsModel = .init(key: "albumType", value: "0", isRequired: false, desc: "相册类型，0：个人")
        let params_2: WmcParamsModel = .init(key: "busId", value: WmcLoginManager.shared.yupaoAccountModel?.userId ?? "", isRequired: false, desc: "用户id，albumType为0时是鱼泡用户id")
        let params_3: WmcParamsModel = .init(key: "resourceType", value: "0", isRequired: false, desc: "资源类型 0：图片  1：视频   不传：所有")

        let result = [params_1, params_2, params_3]
        return result
    }()

    private lazy var api_15: [WmcParamsModel] = {
        let params_1: WmcParamsModel = .init(key: "albumType", value: "0", isRequired: true, desc: "相册类型，0：个人")
        let params_2: WmcParamsModel = .init(key: "busId", value: WmcLoginManager.shared.yupaoAccountModel?.userId ?? "", isRequired: true, desc: "用户id，albumType为0时是鱼泡用户id")
        let params_3: WmcParamsModel = .init(key: "resourceType", value: "", isRequired: false, desc: "资源类型 0：图片  1：视频   不传：所有")

        let result = [params_1, params_2, params_3]
        return result
    }()

    private lazy var api_16: [WmcParamsModel] = {
        let params_1: WmcParamsModel = .init(key: "albumType", value: "0", isRequired: true, desc: "相册类型，0：个人")
        let params_2: WmcParamsModel = .init(key: "busId", value: WmcLoginManager.shared.yupaoAccountModel?.userId ?? "", isRequired: true, desc: "用户id，albumType为0时是鱼泡用户id")
        let params_3: WmcParamsModel = .init(key: "wmIds", value: "", isRequired: false, desc: "水印id")
        let params_4: WmcParamsModel = .init(key: "resourceType", value: "", isRequired: false, desc: "资源类型 0：图片  1：视频   不传：所有")

        let result = [params_1, params_2, params_3, params_4]
        return result
    }()

    private lazy var api_17: [WmcParamsModel] = {
        let params_1: WmcParamsModel = .init(key: "albumType", value: "0", isRequired: true, desc: "相册类型，0：个人")
        let params_2: WmcParamsModel = .init(key: "busId", value: WmcLoginManager.shared.yupaoAccountModel?.userId ?? "", isRequired: true, desc: "用户id，albumType为0时是鱼泡用户id")
        let params_3: WmcParamsModel = .init(key: "maxDate", value: "2022-06-25", isRequired: false, desc: "最大时间")
        let params_4: WmcParamsModel = .init(key: "date", value: "", isRequired: false, desc: "日期")

        let result = [params_1, params_2, params_3]
        return result
    }()

    private func saveShareInfo() {
        WmcHudManager.toast("不知道这接口是干啥子用的，看不懂所以还没写！")
    }

    private func checkoutRequired() -> Bool {
        var result = true
        for (_, model) in dataSource.enumerated() {
            if model.isRequired, model.value.isEmpty {
                result = false
                break
            }
        }
        return result
    }

    // MARK: 字符串转字典

    private func stringConvertToDict(_ str: String) -> [String: Any]? {
        let data = str.data(using: String.Encoding.utf8)
        if let dict = try? JSONSerialization.jsonObject(
            with: data!,
            options: .mutableContainers) as? [String: Any]
        {
            return dict
        }

        return nil
    }

    private func fetchApplyUpload() {
        var params: [String: Any] = [:]
        dataSource.forEach { model in
            if !model.value.isEmpty {
                params[model.key] = model.value
            }
        }
        WmcHudManager.loading()
        WmcMediaUploadAPI.applyUploadUrl(paramsDict: params).subscribe(onNext: { r in
            WmcHudManager.dismiss()
        }, onError: { err in
            WmcHudManager.dismiss()
        }).disposed(by: rx.disposeBag)
    }

    private func fetchBatchApplyUpload() {
        var params: [String: Any] = [:]
        dataSource.forEach { model in
            if !model.value.isEmpty {
                params[model.key] = model.value
            }
        }
        WmcHudManager.loading()
        WmcMediaUploadAPI.applyBatchUploadUrl(paramsDictList: [params, params, params]).subscribe(onNext: { r in
            WmcHudManager.dismiss()
        }, onError: { err in
            WmcHudManager.dismiss()
        }).disposed(by: rx.disposeBag)
    }

    private func fetchApplyCopy() {
        var params: [String: Any] = [:]
        dataSource.forEach { model in
            if !model.value.isEmpty {
                params[model.key] = model.value
            }
        }
        let model: WmcMediaCopyResourceParameterModel = params.parseModelWithClass(WmcMediaCopyResourceParameterModel.self)

        WmcHudManager.loading()
        WmcMediaUploadAPI.copyResource(model, appId: "", resourceId: "", secretKey: "").subscribe(onNext: { r in
            WmcHudManager.dismiss()
        }, onError: { err in
            WmcHudManager.dismiss()
        }).disposed(by: rx.disposeBag)
    }

    private func fetchResourceAccess() {
        var paramsList: [String] = []
        dataSource.forEach { model in
            if !model.value.isEmpty {
                paramsList.append(model.value)
            }
        }

        WmcHudManager.loading()
        WmcMediaUploadAPI.applyResourceAccess(urls: paramsList).subscribe(onNext: { r in
            WmcHudManager.dismiss()
        }, onError: { err in
            WmcHudManager.dismiss()
        }).disposed(by: rx.disposeBag)
    }

    private func fetchConfigs() {
        WmcHudManager.loading()
        WmcMediaConfigAPI.fetchConfig().subscribe(onNext: { r in
            WmcHudManager.dismiss()
        }, onError: { err in
            WmcHudManager.dismiss()
        }).disposed(by: rx.disposeBag)
    }

    private func fetchWatermarkList() {
        WmcHudManager.loading()
    }

    private func fetchBranchSearch() {
        var name: String = ""
        var page = 1
        var pageSize = 10
        dataSource.forEach { model in
            if model.key == "name" {
                name = model.value
            } else if model.key == "currentPage" {
                page = Int(model.value) ?? 1
            } else if model.key == "pageSize" {
                pageSize = Int(model.value) ?? 10
            }
        }

        WmcHudManager.loading()
        WmcMediaWatermarkAPI.fetchEnterpriseLogo(name, page: page, pageSize: pageSize).subscribe(onNext: { r in
            WmcHudManager.dismiss()
        }, onError: { err in
            WmcHudManager.dismiss()
        }).disposed(by: rx.disposeBag)
    }

    private func fetchWmShare() {
        var watermarkInfo: [String: Any] = [:]
        var url: String = ""
        dataSource.forEach { model in
            if model.key == "wmcInfo" {
                if let dic = stringConvertToDict(model.value) {
                    watermarkInfo = dic
                }
            } else if model.key == "wmUrl" {
                url = model.value
            }
        }
        
        let dict: [String: Any] = [
            "wmId": 49,
            "classifyId": 17,
            "name": "内务检查",
            "opacity": 0,
            "size": 50,
            "img": "http://cdn.cdypkj.cn/wmc/template/35021648720190.png",
            "icon": "http://cdn.cdypkj.cn/wmc/icon/62261648720827.png",
            "supportAlbum": false,
            "styleCode": 10,
            "sort": 5,
            "fields": [[
                "fieldId": 458,
                "title": "标题",
                "content": "内务检查",
                "editTitle": false,
                "editContent": true,
                "type": "title",
                "isOpen": true,
                "isAction": false,
                "textColor": "",
                "bgColor": "",
                "sort": 1,
                "brandAttribute": "null",
            ]],
        ]

        WmcHudManager.loading()
        WmcMediaWatermarkAPI.fetchWatermarkShare(watermarkInfo: [:], watermarkUrl: "").subscribe(onNext: { r in
            WmcHudManager.dismiss()
        }, onError: { err in
            WmcHudManager.dismiss()
        }).disposed(by: rx.disposeBag)
    }

    private func fetchWmTemple() {
        var code: String = ""
        dataSource.forEach { model in
            if model.key == "code" {
                code = model.value
            }
        }

        WmcHudManager.loading()
        WmcMediaWatermarkAPI.fetchWatermarkTemplate(code: code).subscribe(onNext: { r in
            WmcHudManager.dismiss()
        }, onError: { err in
            WmcHudManager.dismiss()
        }).disposed(by: rx.disposeBag)
    }

    private func fetchImageUploadApply() {
        var params: [String: Any] = [:]
        dataSource.forEach { model in
            if !model.value.isEmpty {
                params[model.key] = model.value
            }
        }
        let model = params.parseModelWithClass(WmcMediaUploadImageParameterModel.self)
        WmcHudManager.loading()
        WmcMediaUploadAPI.applyUploadImageUrl(model).subscribe(onNext: { r in
            WmcHudManager.dismiss()
        }, onError: { err in
            WmcHudManager.dismiss()
        }).disposed(by: rx.disposeBag)
    }

    private func fetchAlbumList() {
        var page: Int = 1
        var pageSize: Int = 10

        dataSource.forEach { model in
            if model.key == "currentPage" {
                page = Int(model.value) ?? 0
            } else if model.key == "pageSize" {
                pageSize = Int(model.value) ?? 0
            }
        }

        WmcHudManager.loading()
        WmcMediaAlbumAPI.fetchPhotoList(page: page, pageSize: pageSize).subscribe(onNext: { r in
            WmcHudManager.dismiss()
        }, onError: { err in
            WmcHudManager.dismiss()
        }).disposed(by: rx.disposeBag)
    }

    private func deleteImage() {
        var id: Int = 0

        dataSource.forEach { model in
            if model.key == "id" {
                id = Int(model.value) ?? 0
            }
        }

        WmcHudManager.loading()
        WmcMediaAlbumAPI.deletePhoto(id: id).subscribe(onNext: { r in
            WmcHudManager.dismiss()
        }, onError: { err in
            WmcHudManager.dismiss()
        }).disposed(by: rx.disposeBag)
    }

    private func fetchTakeDate() {
        var startDate: String = ""
        var endDate: String = ""

        dataSource.forEach { model in
            if model.key == "startDate" {
                startDate = model.value
            } else if model.key == "endDate" {
                endDate = model.value
            }
        }

        WmcHudManager.loading()
        WmcMediaAlbumAPI.fetchTakePhotoCalendar(startDate: startDate, endDate: endDate).subscribe(onNext: { r in
            WmcHudManager.dismiss()
        }, onError: { err in
            WmcHudManager.dismiss()
        }).disposed(by: rx.disposeBag)
    }

    private func fetchClassifyAlbum() {
        var resourceType: Int = 0

        dataSource.forEach { model in
            if model.key == "resourceType" {
                resourceType = Int(model.value) ?? 0
            }
        }

        WmcHudManager.loading()
        WmcMediaAlbumAPI.fetchAlbumList(resourceType: [resourceType]).subscribe(onNext: { r in
            WmcHudManager.dismiss()
        }, onError: { err in
            WmcHudManager.dismiss()
        }).disposed(by: rx.disposeBag)
    }

    private func fetchAlbumWatermarkList() {
        var resourceType: Int = 0

        dataSource.forEach { model in
            if model.key == "resourceType" {
                resourceType = Int(model.value) ?? 0
            }
        }

        WmcHudManager.loading()
        WmcMediaAlbumAPI.fetchTakePhotoStatistics(resourceType: [resourceType]).subscribe(onNext: { r in
            WmcHudManager.dismiss()
        }, onError: { err in
            WmcHudManager.dismiss()
        }).disposed(by: rx.disposeBag)
    }

    private func fetchLastestTakeDate() {
        var albumType: Int = 0
        var businessId: String = ""
        var maxDate: String = ""
        var resourceType: Int = 0

        dataSource.forEach { model in
            if model.key == "albumType" {
                albumType = Int(model.value) ?? 0
            } else if model.key == "busId" {
                businessId = model.value
            } else if model.key == "maxDate" {
                maxDate = model.value
            } else if model.key == "resourceType" {
                resourceType = Int(model.value) ?? 0
            }
        }

        WmcHudManager.loading()
        WmcMediaAlbumAPI.fetchTakePhotoLastDate(albumType: albumType, businessId: businessId, maxDate: maxDate, resourceType: [resourceType]).subscribe(onNext: { r in
            WmcHudManager.dismiss()
        }, onError: { err in
            WmcHudManager.dismiss()
        }).disposed(by: rx.disposeBag)
    }
}
